/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 有点呆
 * Date: 2024-10-31
 * Time: 16:09
 */

import java.util.*;
import java.text.SimpleDateFormat;

public class ShortJobFirstUtil {

    private static SimpleDateFormat tm = new SimpleDateFormat("HH:mm:ss");

    @SuppressWarnings("resource")
    public static Job init(Job job){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入作业名称：如（作业1）");
        job.setJobName(scanner.nextLine());
        System.out.println("请输入作业到达时间：如（1）");
        job.setJobArrivalTime(scanner.nextInt());
        System.out.println("请输入作业服务时间：如（3）");
        job.setJobServiceTime(scanner.nextInt());
        return job;

    }

    public static void sortByServerTime(List<Job> jobs){
        Collections.sort(jobs, new Comparator<Job>() {

            public int compare(Job job1, Job job2){
                        return Integer.compare(job1.getJobServiceTime(), job2.getJobServiceTime());
                    }
                });
    }

    public static void sortByArrivalTime(List<Job> jobs){
        Collections.sort(jobs, new Comparator<Job>() {
                public int compare(Job job1, Job job2){
                    return Integer.compare(job1.getJobArrivalTime(), job2.getJobArrivalTime());
                }
            });
    }

    public static List<Job> dispatchJob(List<Job> jobs,List<Job> execJobs){
        List<Job> tempJobs = new ArrayList<>();
        ShortJobFirstUtil.sortByArrivalTime(jobs);
        execJobs.add(jobs.remove(0));

        while(!jobs.isEmpty()){
            Job execJob = execJobs.get(execJobs.size() - 1);
            int endTime = execJob.getJobArrivalTime() + execJob.getJobServiceTime();

            Iterator<Job> iterator = jobs.iterator();

            while(iterator.hasNext()){
                Job job = iterator.next();

                if(endTime >= job.getJobArrivalTime()) {
                    tempJobs.add(job);
                    iterator.remove();
                }
            }

            ShortJobFirstUtil.sortByServerTime(tempJobs);
            execJobs.addAll(tempJobs);
            tempJobs.clear();
        }
        return execJobs;
    }

    public static void turnRoundTime(List<Job> jobs){
        int temp = jobs.get(0).getJobArrivalTime();
        for(Job job : jobs){
            if(temp < job.getJobArrivalTime()){
                temp = job.getJobArrivalTime();
            }

            job.setJobStartTime(temp);
            int serviceTime = job.getJobServiceTime();
            temp += serviceTime;
            job.setJobOverTime(temp);
            int turnRound = temp - job.getJobArrivalTime();
            job.setJobRoundTime(turnRound);
            job.setJobAvgRoundTime((1.0 * turnRound) / serviceTime);
        }
    }

    public static void showTime(List<Job> jobs){
        System.out.println("作业名称\t\t到达时间\t\t服务时间\t\t开始时间\t\t结束时间\t\t周转时间\t\t带权周转时间");
        double totalTurnRound = 0.0;
        double totalAvgTurnRound = 0.0;
        for (Job job : jobs){
            System.out.println("\t"+job.getJobName()+"\t\t\t"+job.getJobArrivalTime()+"\t\t\t"+job.getJobServiceTime()
                    +"\t\t\t"+job.getJobStartTime()+"\t\t\t"+job.getJobOverTime()+"\t\t\t"+job.getJobRoundTime()
                    +"\t\t\t"+job.getJobAvgRoundTime());
            totalTurnRound += job.getJobRoundTime();
            totalAvgTurnRound += job.getJobAvgRoundTime();
        }
        System.out.println(jobs.size()+"个作业的平均周转时间为："+String.format("%g %n",(1.0*totalTurnRound)/jobs.size()));
        System.out.println(jobs.size()+"个作业的平均带权周转时间为："+String.format("%g %n",(1.0*totalAvgTurnRound)/jobs.size()));
    }

    //模拟作业到达时间的过程
    public static void sleepJob(List<Job> jobs){
        long l = System.currentTimeMillis();
        ShortJobFirstUtil.sortByArrivalTime(jobs);
        for (Job job : jobs){
            try {
                Thread.sleep(job.getJobArrivalTime() * 1000);
                System.out.print("作业执行情况为："+job.getJobName()+"在"+tm.format(new Date())+"到达！");
                Thread.sleep(job.getJobServiceTime()*1000);
                System.out.println("---在"+tm.format(new Date())+"结束作业！");
            } catch (InterruptedException e) {
                System.out.println("系统出错！！请退出重试！！！");
            }
        }
    }
}
